package elitech.vietnam.myfashion.extend;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.dialog.ShippingDialog;
import elitech.vietnam.myfashion.dialog.WarningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.OrderDetail;
import elitech.vietnam.myfashion.view.OrderDetailView;
import elitech.vietnam.myfashion.view.OrderDetailView.OrderDetailViewEvent;
import elitech.vietnam.myfashion.wsclient.Utilities;

public class OrderArrayAdapter extends ArrayAdapter<Order> {
	
	public static final int CART = 0;
	public static final int STATUS = 1;
	public static final int SUCCESS = 2;
	
	private MainActivity mContext;
	private int mType;
	public OrderArrayAdapter(MainActivity context, int resource, List<Order> objects, int type) {
		super(context, resource, objects);
		this.mContext = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Order item = getItem(position);
		LayoutInflater inflater = mContext.getLayoutInflater();
		View row = inflater.inflate(R.layout.item_order, null, false);
		
		TextView mTxtCode = (TextView) row.findViewById(R.id.order_txtCode);
		TextView mTxtDate = (TextView) row.findViewById(R.id.order_txtDate);
		final TextView mTxtTotal = (TextView) row.findViewById(R.id.order_txtTotal);
		TextView mTxtStatus = (TextView) row.findViewById(R.id.order_txtStatus);
		LinearLayout mlayTitle = (LinearLayout) row.findViewById(R.id.order_layTitle);
		LinearLayout mlayControl = (LinearLayout) row.findViewById(R.id.order_layControl);
		LinearLayout mlayCancel = (LinearLayout) row.findViewById(R.id.order_layCancel);
		final LinearLayout mLayOrder = (LinearLayout) row.findViewById(R.id.order_layOrder);
		LinearLayout mlayDate = (LinearLayout) row.findViewById(R.id.order_layDate);
		LinearLayout mlayStatus = (LinearLayout) row.findViewById(R.id.order_layStatus);
		Button mBtnClear = (Button) row.findViewById(R.id.order_btnClear);
		Button mBtnPurchase = (Button) row.findViewById(R.id.order_btnPurchase);
		Button mBtnCancel = (Button) row.findViewById(R.id.order_btnCancel);
//		ListView mListViewDetail = (ListView) row.findViewById(R.id.order_listDetail);
		
		if(mType == CART){
			mlayTitle.setVisibility(View.GONE);
			mlayControl.setVisibility(View.VISIBLE);
		}else if(mType == STATUS){
			mlayTitle.setVisibility(View.VISIBLE);
			mlayDate.setVisibility(View.VISIBLE);
			mlayStatus.setVisibility(View.VISIBLE);
			if(item.Status==0){
				mTxtStatus.setText(R.string.uncofirm);
				mTxtStatus.setTextColor(mContext.getResources().getColor(R.color.red));
				mlayCancel.setVisibility(View.VISIBLE);
			}else{
				mTxtStatus.setText(R.string.cofirmed);
				mTxtStatus.setTextColor(mContext.getResources().getColor(R.color.green));
			}
		}else if(mType == SUCCESS){
			mlayTitle.setVisibility(View.VISIBLE);
			mlayDate.setVisibility(View.VISIBLE);
			mlayStatus.setVisibility(View.VISIBLE);
			mTxtStatus.setText(R.string.success);
			mTxtStatus.setTextColor(mContext.getResources().getColor(R.color.green));
			mlayCancel.setVisibility(View.VISIBLE);
			mBtnCancel.setText(R.string.delete);
		}
		
		
		if(mType != CART)
		{		
			mTxtCode.setText(item.OrderCode);
			mTxtDate.setText(item.date);
		}
		mBtnClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mType == CART){
					mContext.mOrder = new Order();
					notifyDataSetChanged();
				}
			}
		});
		
		mBtnPurchase.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mType == CART){
					if (mContext.mOrder.ListDetail.size() == 0) {
						new WarningDialog(mContext, mContext
								.getString(R.string.emptycart)).show();
						return;
					}
					ShippingDialog dialog = new ShippingDialog(mContext, false);
					dialog.setOnDismissListener(new OnDismissListener() {
	
						@Override
						public void onDismiss(DialogInterface dialog) {
							notifyDataSetChanged();
							onPurchase();
//							GetOrder();
						}
					});
					dialog.show();
				}
			}
		});

		mBtnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mType == STATUS){
					new OkCancelWarnningDialog(mContext, mContext.getString(R.string.areyousure)){
						public void onOk() {
							new CallServiceTask(){
								int rs = -1;
								public void work() {
									try {
										rs = service.deleteOrder(item.Id);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (XmlPullParserException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								};
								
								@Override
								protected void onPostExecute(Void result) {
									if(rs != -1){
										remove(item);
										notifyDataSetChanged();
									}
								}
								
							}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
						};
					}.show();
				}else if(mType == SUCCESS){
					new OkCancelWarnningDialog(mContext, mContext.getString(R.string.areyousure)){
						public void onOk() {
							new CallServiceTask(){
								int rs = -1;
								public void work() {
									try {
										rs = service.deleteOrder1(item.Id);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (XmlPullParserException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								};
								
								@Override
								protected void onPostExecute(Void result) {
									if(rs != -1 && rs != 0){
										remove(item);
										notifyDataSetChanged();
									}
								}
								
							}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
						};
					}.show();
				}
			}
		});
		
		double mTotal = 0;
		
		for (final OrderDetail detail : item.ListDetail) {
			final OrderDetailView view = new OrderDetailView(mContext, detail);
			mTotal = mTotal + (detail.getAmount());
			if(item.Status==-100){
				view.mBtnDelete.setVisibility(View.VISIBLE);
				view.setOnCancelEvent(new OrderDetailViewEvent() {
					
					@Override
					public void onCancel() {
						if (item.ListDetail.size() > 1) {
							new WarningDialog(mContext, mContext
									.getString(R.string.confirmdeletebill)) {
								public void onClick(View v) {
									mLayOrder.removeView(view);
//									item.ListDetail.remove(detail);
									mContext.mOrder.ListDetail.remove(detail);
									notifyDataSetChanged();
									super.onClick(v);
								};
							}.show();
						} else {
							new WarningDialog(mContext, mContext
									.getString(R.string.confirmdeletebill)) {
								public void onClick(View v) {
									mContext.mOrder = new Order();
									notifyDataSetChanged();
									super.onClick(v);
								};
							}.show();
						}
					}
				});
			}
			mLayOrder.addView(view);
		}
		
		String total = (item.Total == 0)? Utilities.numberFormat(mTotal) + " VNĐ": Utilities.numberFormat(item.Total) + " VNĐ";
		mTxtTotal.setText(total);
		
		return row;
	}
	
	public void onPurchase(){}
	
}
